//
//  MovieDetailViewController.m
//  豆瓣_精简版
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieDetailViewController.h"
#import "DetailView.h"
#import "AppTools.h"
#import "MovieDetail.h"
#import "DataBase.h"
#import "LoginViewController.h"
@interface MovieDetailViewController ()

@property(nonatomic,retain)DetailView *detailV;

@end

@implementation MovieDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    //左右按钮
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIImage *rightImage = [UIImage imageNamed:@"btn_nav_share.png"];
    rightImage = [rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightImage style:UIBarButtonItemStyleDone target:self action:@selector(rightButtonAction:)];
    
    //吧当前页面加到scrollview上
    UIScrollView *myScrollView = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    myScrollView.contentSize = CGSizeMake(375, 900);
    [self.view addSubview:myScrollView];
    
    self.detailV = [[DetailView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    [myScrollView addSubview:self.detailV];
    
    NSString *urlStr = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=%@",self.movie.movieId];
    [AppTools getDataWithURL:urlStr block:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSMutableDictionary *detailDic = [dic objectForKey:@"result"];
        MovieDetail *movieD = [[MovieDetail alloc]init];
        [movieD setValuesForKeysWithDictionary:detailDic];
        movieD.movieId = self.movie.movieId;
       self.detailV.movieDetail = movieD;
        
        
        //内容自适应
        CGFloat height = [DetailView heightForContent:movieD.plot_simple width:340];
        CGRect tempframe = self.detailV.plot_simple.frame;
        tempframe.size.height = height;
        self.detailV.plot_simple.frame = tempframe;
        myScrollView.contentSize = CGSizeMake(375, 450 + height);
    }];
    
    
    [[DataBase shareDataBase]createMovieTable];
    //[[DataBase shareDataBase] dropTable:@"movie"];
}

//返回按钮实现
- (void)leftButtonAction:(UIBarButtonItem *)bar{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

//收藏按钮
- (void)rightButtonAction:(UIBarButtonItem *)bar{
    
    NSString *state = [[NSUserDefaults standardUserDefaults]objectForKey:@"state"];
    if ([state isEqualToString:@"0"]) {
        
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        [self presentViewController:naVC animated:YES completion:^{
            
            
        }];
        
    }else{
        NSString *name = [[NSUserDefaults standardUserDefaults]objectForKey:@"user"];
        NSMutableArray *array = [[DataBase shareDataBase]selectMovieUserData:name movieName:self.movie.movieName];
        
        if (array.count == 0) {
            self.movie.userName = name;
            [[DataBase shareDataBase]insertMovieData:self.movie];
        }else{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经收藏过了" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
            }];
            [alert addAction:action];
        }
    }
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
